﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TSL.AjaxResult.KaiJiang
{
    public class KaiJiang3
    {
        public static int OpenJJ(string caipiao_name, string codes, string buycodes)
        {
            switch (caipiao_name)
            {
                case "任二":
                    return RenEr(codes, buycodes);

                case "任三":
                    return RenSan(codes, buycodes);

                case "任四":
                    return RenSi(codes, buycodes);

                case "任五":
                    return RenWu(codes, buycodes);

                case "前一数投":
                    return QianYiST(codes, buycodes);

                case "前一红投":
                    return QianYiHT(codes, buycodes);

                case "选二连直":
                    return XuanErLZ(codes, buycodes);

                case "选三前直":
                    return XuanSanQZ(codes, buycodes);

                case "选二连组":
                    return XuanErLZU(codes, buycodes);

                case "选三连组":
                    return XuanSanQZU(codes, buycodes);
            }
            return 0;
        }

        public static int QianYiHT(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                string[] mycodes = mycode[i].Split(new char[] { ',' });
                for (int j = 0; j < mycodes.Length; j++)
                {
                    _code = code[0].ToString().Trim() + " ";
                    if ((mycodes[j].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                winbonus += win;
            }
            return winbonus;
        }

        public static int QianYiST(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                string[] mycodes = mycode[i].Split(new char[] { ',' });
                for (int j = 0; j < mycodes.Length; j++)
                {
                    _code = code[0].ToString().Trim() + " ";
                    if ((mycodes[j].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                winbonus += win;
            }
            return winbonus;
        }

        public static int RenEr(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                for (int j = 0; j < code.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if ((mycode[i].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                if (win >= 2)
                {
                    winbonus += (win * (win - 1)) / 2;
                }
            }
            return winbonus;
        }

        public static int RenSan(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                for (int j = 0; j < code.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if ((mycode[i].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                if (win >= 3)
                {
                    winbonus += (win * (win - 1)) / 6;
                }
            }
            return winbonus;
        }

        public static int RenSi(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                for (int j = 0; j < code.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if ((mycode[i].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                if (win >= 4)
                {
                    winbonus += (win * (win - 1)) / 12;
                }
            }
            return winbonus;
        }

        public static int RenWu(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int winbonus = 0;
            int win = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                for (int j = 0; j < code.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if ((mycode[i].ToString().Trim() + " ").IndexOf(_code) != -1)
                    {
                        win++;
                    }
                }
                if (win == 5)
                {
                    winbonus++;
                }
            }
            return winbonus;
        }

        public static int XuanErLZ(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string mycodes = string.Empty;
            string _code = string.Empty;
            int win = 0;
            int winbonus = 0;
            int P = 0;
            int W = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                P = 0;
                W = 0;
                mycodes = mycode[i].ToString();
                for (int j = 0; j < mycodes.Length; j++)
                {
                    _code = code[j].ToString().Trim();
                    if ((mycodes.Substring(j, 1).IndexOf(_code) != -1) && (mycodes.Length == 2))
                    {
                        win++;
                    }
                    if (win == 1)
                    {
                        P = j;
                    }
                    if (win == 2)
                    {
                        W = j;
                    }
                }
                if ((win == 2) && ((P + 1) == W))
                {
                    winbonus++;
                }
            }
            return winbonus;
        }

        public static int XuanErLZU(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode = buycodes.Split(new char[] { '$' });
            string mycodes = string.Empty;
            string _code = string.Empty;
            string _mycode = string.Empty;
            int win = 0;
            int winbonus = 0;
            int P = 0;
            int W = 0;
            for (int i = 0; i < mycode.Length; i++)
            {
                win = 0;
                P = 0;
                W = 0;
                mycodes = mycode[i].ToString();
                for (int j = 0; j < code.Length; j++)
                {
                    _code = code[j].ToString().Trim();
                    _mycode = mycodes;
                    if (_mycode.IndexOf(_code) != -1)
                    {
                        if ((Math.Abs((int)(P - W)) == 3) || (j == 0))
                        {
                            win++;
                        }
                        if (win == 1)
                        {
                            P = _mycode.IndexOf(_code);
                        }
                        if (win > 1)
                        {
                            W = _mycode.IndexOf(_code);
                        }
                    }
                }
                if (win >= 2)
                {
                    winbonus += (win * (win - 1)) / 2;
                }
            }
            return winbonus;
        }

        public static int XuanSanQZ(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode1 = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int win = 0;
            int winbonus = 0;
            for (int i = 0; i < mycode1.Length; i++)
            {
                win = 0;
                string[] mycode3 = mycode1[i].ToString().Trim().Split(new char[] { ',' });
                for (int j = 0; j < mycode3.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if (((mycode3[j].ToString().Trim() + " ").IndexOf(_code) != -1) && (mycode3.Length == 3))
                    {
                        win++;
                    }
                }
                if (win == 3)
                {
                    winbonus++;
                }
            }
            return winbonus;
        }

        public static int XuanSanQZU(string codes, string buycodes)
        {
            string[] code = codes.Split(new char[] { ',' });
            string[] mycode1 = buycodes.Split(new char[] { '$' });
            string _code = string.Empty;
            int win = 0;
            int winbonus = 0;
            for (int i = 0; i < mycode1.Length; i++)
            {
                win = 0;
                string[] mycode3 = mycode1[i].ToString().Trim().Split(new char[] { ',' });
                for (int j = 0; j < mycode3.Length; j++)
                {
                    _code = code[j].ToString().Trim() + " ";
                    if (((mycode3[j].ToString().Trim() + " ").IndexOf(_code) != -1) && (mycode3.Length == 3))
                    {
                        win++;
                    }
                }
                if (win == 3)
                {
                    winbonus++;
                }
            }
            return winbonus;
        }
    }
}
